/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.planeteVelo.bean;

import com.planeteVelo.backedBean.Categorie;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 *
 * @author guig
 */
@Named
@RequestScoped
public class CategorieBean {

    private Categorie categorie = new Categorie();
    private List<Categorie> lesCategories = new ArrayList();
    @PersistenceContext
    EntityManager em;
    @Resource
    UserTransaction tx;

    @PostConstruct
    public void init() {

        Query query = em.createQuery("SELECT ca FROM Categorie ca");
        lesCategories = query.getResultList();

    }
    
    public void ajouterCategorie(){
        System.out.println(this.categorie.getNomCategorie());
        FacesMessage message = null;

        message = new FacesMessage("Ajout validé");
        //ajouter le message JSF
        FacesContext.getCurrentInstance().addMessage(null, message);
        try {
            tx.begin();
            em.persist(this.categorie);
            tx.commit();
            em.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        init();
    }

    public void modifierCategorie(Categorie ca){
        FacesMessage message = null;
    message = new FacesMessage("Modification validée");
    //ajouter le message JSF
    FacesContext.getCurrentInstance().addMessage(null, message);
        try {
            tx.begin();
            em.persist(em.merge(ca));
            tx.commit();
            
        }
         catch(Exception ex)
                {
                System.out.println(ex);
                }
    }

public void supprimerCategorie(Categorie ca) {
    FacesMessage message = null;
    message = new FacesMessage("Suppression validée");
    //ajouter le message JSF
    FacesContext.getCurrentInstance().addMessage(null, message);
        try {
            tx.begin();
            em.remove(em.merge(ca));
            tx.commit();
            em.close();
        }
         catch(Exception ex)
                {
                System.out.println(ex);
                }
      init();
}

    public Categorie getCategorie() {
        return categorie;
    }

    public void setCategorie(Categorie categorie) {
        this.categorie = categorie;
    }

    public List<Categorie> getLesCategories() {
        return lesCategories;
    }

    public void setLesCategories(List<Categorie> lesCategories) {
        this.lesCategories = lesCategories;
    }


    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    public UserTransaction getTx() {
        return tx;
    }

    public void setTx(UserTransaction tx) {
        this.tx = tx;
    }
    
}
